//
//  KMFileManager.h
//  CNstorm
//
//  Created by 陈家将 on 16/7/17.
//  Copyright © 2016年 chenjiajiang. All rights reserved.
//

#import "KMFileManager.h"

@implementation KMFileManager
/** 清除缓存 */
+ (void)removeDirectoryPath:(NSString *)directoryPath
{
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    BOOL isDirectory;
#ifdef DEBUG //在调试状态下抛出异常
    //参数路径不存在 或 不是文件夹 时
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    if (!isExist || !isDirectory) {
        // 报错:抛异常
        NSException *excp = [NSException exceptionWithName:@"filePathError" reason:@"参数错误,必须传文件夹路径" userInfo:nil];
        
        [excp raise];
        
    }
#endif
    // 获取文件夹下所有的文件
    NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
    
    for (NSString *subPath in subpaths) {
        //拼接全路径
        NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
        //删除文件
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    }
}


/** 获取文件夹内总资源大小 */
+ (NSInteger)getDirectorySize:(NSString *)directoryPath{
    //获取文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    //是否是文件夹
    BOOL isDirectory;
    //文件是否存在
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    //参数路径不存在 或 不是文件夹 时
#ifdef DEBUG //在调试状态下抛出异常
    if (!isExist || !isDirectory) {
        // 报错:抛异常
        NSException *excp = [NSException exceptionWithName:@"filePathError" reason:@"参数错误,必须传文件夹路径" userInfo:nil];
        [excp raise];
        
    }
#endif
    /*
     获取这个文件夹中所有文件路径,然后累加 = 文件夹的尺寸
     */

    // 获取文件夹下所有的文件
    NSArray *subpaths = [mgr subpathsAtPath:directoryPath];
    NSInteger totalSize = 0;
    
    for (NSString *subpath in subpaths) {
        
        // 拼接文件全路径
        NSString *filePath = [directoryPath stringByAppendingPathComponent:subpath];
        
        // 排除文件夹
        BOOL isDirectory;
        BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
        if (!isExist || isDirectory) continue;
        
        // 隐藏文件
        if ([filePath containsString:@".DS"]) continue;
        
        // 指定路径获取这个路径的属性
        // attributesOfItemAtPath:只能获取文件属性
        NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];
        NSInteger size = [attr fileSize];
        
        totalSize += size;
    }
    
    return totalSize;
}

/** 获取文件尺寸字符串 */
+ (NSString *)getFileSizeStr:(NSString *)directoryPath prefixStr:(NSString *)prefixStr
{
    // 获取Cache文件夹路径
    
    // b -> b / 1000 KB -> KB / 1000 MB (手机的是1000)
    // 获取文件夹尺寸
    NSInteger totalSize = [self getDirectorySize:directoryPath];
    NSString *str;
    if (totalSize > 1000 * 1000) { // MB
        CGFloat totalSizeF = totalSize / 1000.0 / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fM)",prefixStr,totalSizeF];
    } else if (totalSize > 1000) { // KB
        CGFloat totalSizeF = totalSize / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fK)",prefixStr,totalSizeF];
    } else if (totalSize >= 0) { // B
        str = [NSString stringWithFormat:@"%@(%ldB)",prefixStr,totalSize];
    }
    
    return str;
}
@end
